// ActionScript file
import Components.TitleWindow.twReadName;

import flash.events.Event;

import mx.controls.Alert;
import mx.managers.PopUpManager;

import org.flexlancer.Logger.Logger;
import org.flexlancer.databases.AlertActions;
import org.flexlancer.databases.AlertTypes;
import org.flexlancer.databases.Alerts;
import org.flexlancer.events.NameEntered;

[Bindable]
private var dpAlertType:Array;
[Bindable]
private var dpAlertAction:Array;
[Bindable]
private var dpAlertDate:Date;

public var customerid:String;

private function OnCC():void{
	try{
		this.dpAlertType = AlertTypes.List();
		this.dpAlertAction = AlertActions.List();	
		var dtNow:Date = new Date();
		
		dtNow.setTime( dtNow.getTime() + 7*24*60*60*1000 );
		this.dpAlertDate = dtNow;
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OnAddAlertType():void{
	try{
		var addAlertType:twReadName = PopUpManager.createPopUp(this,twReadName,true) as twReadName;
		addAlertType.addEventListener(NameEntered.NAME_ENTERED,OnEnterAlertTypeName);
		PopUpManager.centerPopUp(addAlertType);
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OnEnterAlertTypeName(evt:NameEntered):void{
	try{
		AlertTypes.Add(evt.Name);
		this.dpAlertType = AlertTypes.List();
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OnDeleteAlertType():void{
	try{
		if ( this.lstAlertType.selectedItem != null ){
			AlertTypes.Delete("type LIKE '" + this.lstAlertType.selectedItem.type + "'");
			this.dpAlertType = AlertTypes.List();
		}
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OnAddAlertAction():void{
	try{
		var addAlertAction:twReadName = PopUpManager.createPopUp(this,twReadName,true) as twReadName;
		addAlertAction.addEventListener(NameEntered.NAME_ENTERED,OnEnterAlertActionName);
		PopUpManager.centerPopUp(addAlertAction);
	}catch(err:Error){
		Logger.LogE(err);
	}
}
private function OnEnterAlertActionName(evt:NameEntered):void{
	try{
		AlertActions.Add(evt.Name);
		this.dpAlertAction = AlertActions.List();
	}catch(err:Error){
		Logger.LogE(err);
	}
}
private function OnDeleteAlertAction():void{
	try{
		if ( this.lstAlertAction.selectedItem != null ){
			AlertActions.Delete("action LIKE '" + this.lstAlertAction.selectedItem.action + "'");
			this.dpAlertAction = AlertActions.List();
		}
	}catch(err:Error){
		Logger.LogE(err);
	}
}
public static const ALERT_ADDED:String = "alertAdded";
private function OnSave():void{
	try{
		if ( this.lstAlertType.selectedItem == null ){
			Alert.show("Please select type of alert.");
			return;
		}
		
		if ( this.lstAlertAction.selectedItem == null ){
			Alert.show("Please select action for alert.");
			return;
		}
		
		if ( this.dfAlertDate.selectedDate == null ){
			Alert.show("Please select date for alert.");
			return;
		}
		
		var active:String = (this.chkActive.selected ? "1":"0");
		var dateStr:String = this.dfAlertDate.selectedDate.fullYear + "-" + this.dfAlertDate.selectedDate.month + "-" + this.dfAlertDate.selectedDate.date;
		Alerts.Add(this.lstAlertType.selectedItem.type,dateStr,this.lstAlertAction.selectedItem.action,active,"CUST-"+this.customerid);
		this.dispatchEvent(new Event(ALERT_ADDED));
		Alert.show("Alert added");
		this.OnClose();
	}catch(err:Error){
		Logger.LogE(err);
	}
}